<?php
/**
 * Created by PhpStorm.
 * User: ljg
 * Date: 2020/7/2
 * Time: 19:37
 */

/**
 * @param $num
 * @return string
 * @content 根据数字获取对应的字母
 */
function LetterToNumber($num)
{
    $ABCstr = "";
    $ten = $num;
    if ($ten == 0) return "A";
    while ($ten != 0) {
        $x = $ten % 26;
        $ABCstr .= chr(65 + $x);
        $ten = intval($ten / 26);
    }
    return strrev($ABCstr);
}

/**
 * @return mixed
 * @content 获取客户端真实ip
 */
function getsIp()
{
    $unknown = 'unknown';
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    /*
    处理多层代理的情况
    或者使用正则方式：$ip = preg_match("/[\d\.]{7,15}/", $ip, $matches) ? $matches[0] : $unknown;
    */
    if (false !== strpos($ip, ','))
        $ip = reset(explode(',', $ip));
    return $ip;
}

/**
 * @param $id
 * @return false|string
 * @content 通过身份证号获取年龄
 */
function getAge($id)
{

# 1.从身份证中获取出生日期
    $birth_Date = strtotime(substr($id, 6, 8));//截取日期并转为时间戳

# 2.格式化[出生日期]
    $Year = date('Y', $birth_Date);//yyyy
    $Month = date('m', $birth_Date);//mm
    $Day = date('d', $birth_Date);//dd

# 3.格式化[当前日期]
    $current_Y = date('Y');//yyyy
    $current_M = date('m');//mm
    $current_D = date('d');//dd

# 4.计算年龄()
    $age = $current_Y - $Year;//今年减去生日年
    if ($Month > $current_M || $Month == $current_M && $Day > $current_D) {//深层判断(日)
        $age--;//如果出生月大于当前月或出生月等于当前月但出生日大于当前日则减一岁
    }
# 返回
    return $age;

}

/**
 * @param $imgUrl
 * @param $mark
 * @return string
 * @content 图片路径整理(返回 http/https)
 */
function imgUrl($imgUrl, $mark = false)
{
    if (!empty($imgUrl) and !is_null($imgUrl)) {
        if (preg_match('/(http:\/\/)|(https:\/\/)/i', $imgUrl)) {
            return $imgUrl;
        }
        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        if (!$mark) {
            if (file_exists(ROOT_PATH . "upload/" . $imgUrl)) {
                $imgUrl = str_replace('\\', '/', $imgUrl);
                return $http_type . $_SERVER['HTTP_HOST'] . "/upload/" . $imgUrl;
            } else {
                return "";
            }
        } else {
            if (file_exists(ROOT_PATH . $imgUrl)) {
                $imgUrl = str_replace('\\', '/', $imgUrl);
                return $http_type . $_SERVER['HTTP_HOST'] . '/' . $imgUrl;
            } else {
                return "";
            }
        }
    } else {
        return "";
    }
}

/**
 * @param int $begin_time 开始时间戳
 * @param int $end_time 结束时间戳
 * @param boolean $mark 时间格式  true=>时间戳  false=>日期格式
 * @return array
 * @content  计算两个时间戳之间相差的日时分秒
 */
function timediff($begin_time, $end_time, $mark = true)
{
    if ($mark == false) {
        # 日期格式,将时间转为时间戳格式
        $begin_time = strtotime($begin_time);
        $end_time = strtotime($end_time);
    }
    # 判断时间大小
    if ($begin_time < $end_time) {
        $starttime = $begin_time;
        $endtime = $end_time;
    } else {
        $starttime = $end_time;
        $endtime = $begin_time;
    }

    //计算天数
    $timediff = $endtime - $starttime;
    $days = intval($timediff / 86400);
    //计算小时数
    $remain = $timediff % 86400;
    $hours = intval($remain / 3600);
    //计算分钟数
    $remain = $remain % 3600;
    $mins = intval($remain / 60);
    //计算秒数
    $secs = $remain % 60;
    $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
    return $res;
}


/**
 * @param array $logConsumption 消费记录
 * @param string $memberCode 玩家ID
 * @return array
 * @throws \think\Exception
 * @content 消费加积分
 */
function addIntegral_branch($logConsumption, $memberCode)
{
    if (!empty($logConsumption) and !empty($memberCode)) {
        # 加积分,积分记录
        $integralArr = array();
        $integral = 0;
        # 加金币,金币记录
        $goldArr = array();
        $gold = 0;
        foreach ($logConsumption as $k => $v) {
            $integral = $integral + $v['money'];
            array_push($integralArr, array(
                'member_code' => $memberCode,
                'num' => floatval($v['money']),
                'status' => 1,
                'type' => 3,
                'create_time' => $v['time'],
                'order_id' => $v['order_id'],
                'type_branch' => 7,
                'total_money' => floatval($v['total_money'])
            ));
            $gold = $gold + ($v['money'] * 0.75);
            array_push($goldArr, array(
                'member_code' => $memberCode,
                'gold_num' => floatval($v['money'] * 0.75),
                'type' => 3,
                'create_time' => $v['time'],
                'order_id' => $v['order_id'],
                'type_branch' => 8
            ));
        }
        if ($gold > 0) {
            \think\Db::table('member_property')->where(array('member_code' => $memberCode))->setInc('gold', $integral);
            \think\Db::table('log_gold')->insertAll($goldArr);
            $redis = new \Redis\Redis();
            $memberInfo = $redis->hGetJson('memberInfo', strtoupper($memberCode));
            $memberInfo['gold'] = priceFormat($memberInfo['gold'] + $integral);
            $redis->hSetJson('memberInfo', strtoupper($memberCode), $memberInfo);
        }
        if ($integral > 0) {
            \think\Db::table('member_property')->where(array('member_code' => $memberCode))->setInc('integral', $integral);
            \think\Db::table('log_integral')->insertAll($integralArr);
            $redis = new \Redis\Redis();
            $memberInfo = $redis->hGetJson('memberInfo', strtoupper($memberCode));
            $memberInfo['integral'] = priceFormat($memberInfo['integral'] + $integral);
            $redis->hSetJson('memberInfo', strtoupper($memberCode), $memberInfo);
            return array('status' => true, 'msg' => '添加成功');
        } else {
            return array('status' => false, 'msg' => '数据为空 ,添加失败');
        }
    } else {
        return array('status' => false, 'msg' => '参数为空');
    }
}

/**
 * @param $type_branch
 * @return int
 * @content 记录->活动详细类型
 */
function getTypeBranch($type_branch)
{
    switch ($type_branch) {
        case 1:
            # 转轮
            return 1;
            break;
        case 2:
            # 任务
            return 3;
            break;
        case 3:
            # 答题
            return 5;
            break;
        case 4:
            # 签到
            return 4;
            break;
    }
}

/**
 * 本周的开始日期
 * @param bool $His 是否展示时分秒 默认true
 * @return false|string
 */
function beginWeek($His = true)
{
    $timestamp = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y'));
    return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
}

/**
 * 本周的结束日期
 * @param bool $His 是否展示时分秒 默认true
 * @return false|string
 */
function endWeek($His = true)
{
    $timestamp = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'));
    return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
}

/**
 * @param $phone
 * @param $start
 * @param $length
 * @return mixed
 * @content 手机号****加密显示
 */
function hidPhone($phone, $start = 4, $length = 4)
{
    $_start = '*';
    for ($i = 1; $i < $length; $i++) {
        $_start .= '*';
    }
    return substr_replace($phone, $_start, $start, $length);
}

/**
 * @param $sex
 * @return string
 * @content 性别转换
 */
function getSexInfo($sex)
{
    switch ($sex) {
        case '男':
            return 1;
            break;
        case '女':
            return 2;
            break;
    }
}

/**
 * @param $trad_status
 * @return string
 * @content 交易状态
 */
function getTradStatus($trad_status)
{
    switch ($trad_status) {
        case 1:
            return '待确认';
            break;
        case 2:
            return '已确认';
            break;
        case 3:
            return '超时交易';
            break;
    }
}

/**
 * @param $resaon
 * @return string
 * @content 玩家投诉原因
 */
function getComplaintReason($resaon)
{
    switch ($resaon) {
        case 1:
            return '上传虚假图片';
            break;
        case 2:
            return '未收到转账';
            break;
        default:
            return '其他';
            break;
    }
}

/**
 * @param $mark
 * @return string
 * @content 投诉分类
 */
function getComplaintType($mark)
{
    switch ($mark) {
        case 0:
            return '生意交易投诉';
            break;
        case 1:
            return '聊天内投诉';
            break;
        case 2:
            return '交易厅投诉';
            break;
    }
}

/**
 * @param $status
 * @return string
 * @content 玩家投诉-处理状态
 */
function getComplaintStatus($status)
{
    switch ($status) {
        case 1:
            return '待处理';
            break;
        case 2:
            # 通过
            return '已处理';
            break;
        case 3:
            # 驳回
            return '已处理';
            break;
    }
}

/**
 * @param $status
 * @return string
 * @content 玩家投诉-处理状态
 */
function getComplaintHandelStatus($status)
{
    switch ($status) {
        case 0:
            return '待处理';
            break;
        case 1:
            # 通过
            return '审核通过';
            break;
        case 2:
            # 驳回
            return '审核驳回';
            break;
    }
}

/**
 * @param $status
 * @return string
 * @content 合伙人申请-处理结果
 */
function getPartnerAuditStatus($status)
{
    switch ($status) {
        case 1:
            return '待处理';
            break;
        case 2:
            return '审核通过';
            break;
        case 3:
            return '审核驳回';
            break;
    }
}

/**
 * @param $source
 * @return string
 * @content 卡券来源  1 活动奖励\n-1 后台赠送 2.玩家交易
 */
function getVoucherSource($source, $branch)
{
    switch ($source) {
        case 1:
            if ($branch == 0) {
                return '活动奖励';
            } elseif ($branch == 1) {
                return '转轮摇奖';
            } elseif ($branch == 2) {
                return '积分兑换';
            } elseif ($branch == 3) {
                return '完成任务';
            } elseif ($branch == 4) {
                return '签到奖励';
            } elseif ($branch == 5) {
                return '答题奖励';
            } elseif ($branch == 1) {
                return '活动奖励';
            }
            break;
        case 2:
            return '玩家交易';
            break;
        case -1:
            //后台赠送
            return '系统赠送';
            break;
    }
}

/**
 * @param $type
 * @return string
 * @content 金币记录类型
 */
function getGoldType($type, $type_branch)
{
    switch ($type) {
        case 1:
            return '转出';
            break;
        case 2:
            return '转入';
            break;
        case 3:
            return '系统赠送';
            break;
        case 4:
            return '活动奖励';
            break;
        case 5:
            if ($type_branch == 9) {
                return '推荐玩家奖励';
            } elseif ($type_branch == 10) {
                return '推荐达成奖励';
            } else {
                return '推荐奖励';
            }
            break;
        case 6:
            if ($type_branch == 11) {
                return '新增关联玩家';
            } elseif ($type_branch == 12) {
                return '关联玩家达成';
            } else {
                return '联盟推荐';
            }
            break;
        case 7:
            return '市场收益';
            break;
        case 8:
            return '摊位租金';
            break;
        case 9:
            return '合成宝石';
            break;
        case 10:
            return '兑换材料';
            break;
        case 11:
            return '驱逐成功';
            break;
        case 12:
            return '打劫成功';
            break;
        case 13:
            return '打劫失败';
            break;
        case 14:
            return '被打劫成功';
            break;
        case 15:
            return '卡券兑换';
            break;
        case 16:
            return '发红包';
            break;
        case 17:
            return '抢红包';
            break;
        case 18:
            return '积分兑换';
            break;
        case 19:
            return '红包退回';
            break;
        case 20:
            return '金币充值';
            break;
        case 21:
            return '购买生意';
            break;
        case 22:
            return '出售生意';
            break;
        case 23:
            return '出售金币手续费';
            break;
        case 24:
            return '出售金币';
            break;
        case 25:
            return '购买金币';
            break;
        case 26:
            return '系统处罚';
            break;
        case -1:
            //后台赠送
            return '系统修改';
            break;
    }
}

/**
 * @param $type
 * @return string
 * @content 恢复材料类型
 */
function getMaterialsType($type)
{
    switch ($type) {
        case 1:
            return '金币兑换';
            break;
        case 2:
            return '冻结恢复';
            break;
        case 3:
            return '活动奖励';
            break;
        case 4:
            return '积分兑换';
            break;
        case -1:
            //后台赠送
            return '系统修改';
            break;
    }
}

/**
 * @param $type
 * @return string
 * @content 积分记录类型
 */
function getIntegralType($type)
{
    switch ($type) {
        case 1:
            return '升级使用';
            break;
        case 2:
            return '积分兑换';
            break;
        case 3:
            return '系统赠送';
            break;
        case 4:
            return '活动赠送';
            break;
        case 5:
            return '合成宝石';
            break;
        case 6:
            return '摇奖使用';
            break;
        case 7:
            return '转出';
            break;
        case 8:
            return '转入';
            break;
        case 9:
            return '打劫成功';
            break;
        case 10:
            return '被打劫';
            break;
        case 11:
            return '发红包';
            break;
        case 12:
            return '抢红包';
            break;
        case 13:
            return '推荐玩家';
            break;
        case 14:
            return '推荐玩家';
            break;
        case 15:
            return '红包退回';
            break;
        case 16:
            return '出售积分手续费';
            break;
        case 17:
            return '出售积分';
            break;
        case 18:
            return '购买积分';
            break;
        case -1:
            //后台赠送
            return '系统修改';
            break;
    }
}

/**
 * @param $type
 * @return string
 * @content 收益记录类型
 */
function getLogEranType($type)
{
    switch ($type) {
        case 1:
            return '推荐玩家';
            break;
        case 2:
            return '联盟推荐';
            break;
        case 3:
            return '经营收益';
            break;
        case 4:
            return '市场收益';
            break;
        case 5:
            return '平台奖励';
            break;
        case -1:
            //后台修改
            return '系统修改';
            break;
    }
}

/**
 * @param $type
 * @return string
 * @content 好友关系
 */
function getFriendsRelationShip($type)
{
    switch ($type) {
        case 1:
            return '我的推荐人';
            break;
        case 2:
            return '我的直推';
            break;
        case 3:
            return '我的团队';
            break;
        case 4:
            return '我的好友';
            break;
        default:
            return '不是好友';
            break;
    }
}